<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>js数据类型及转换 | 一枚前端小菜鸟的博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="下面代码输出结果1234567[1,2] + [3,4][] + &amp;#123;&amp;#125;&amp;#123;&amp;#125; + []&#x2F;&#x2F; 1. []和&amp;#123;&amp;#125;都是对象，因此调用toString()方法，&amp;#x27;&amp;#x27; + &amp;#x27;[Object Object]&amp;#x27;&#x2F;&#x2F; 2. &amp;#123;&amp;#125;es6中被认为是一个代码块，+[]表示正负数因此被强制转换成数字类型+">
<meta property="og:type" content="article">
<meta property="og:title" content="js数据类型及转换">
<meta property="og:url" content="https://gitee.com/weiyayun1995/weiyayun1995.git/2021/06/22/js%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%8F%8A%E8%BD%AC%E6%8D%A2/index.html">
<meta property="og:site_name" content="一枚前端小菜鸟的博客">
<meta property="og:description" content="下面代码输出结果1234567[1,2] + [3,4][] + &amp;#123;&amp;#125;&amp;#123;&amp;#125; + []&#x2F;&#x2F; 1. []和&amp;#123;&amp;#125;都是对象，因此调用toString()方法，&amp;#x27;&amp;#x27; + &amp;#x27;[Object Object]&amp;#x27;&#x2F;&#x2F; 2. &amp;#123;&amp;#125;es6中被认为是一个代码块，+[]表示正负数因此被强制转换成数字类型+">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://assets.che300.com/wiki/2021-05-18/16213164486292034.png">
<meta property="og:image" content="https://assets.che300.com/wiki/2021-05-09/16205717674285892.png">
<meta property="article:published_time" content="2021-06-22T05:42:36.000Z">
<meta property="article:modified_time" content="2021-06-22T05:43:09.011Z">
<meta property="article:author" content="yywei">
<meta property="article:tag" content="菜鸟,前端,js,react,vue">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://assets.che300.com/wiki/2021-05-18/16213164486292034.png">
  
    <link rel="alternate" href="/atom.xml" title="一枚前端小菜鸟的博客" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.4.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">一枚前端小菜鸟的博客</a>
      </h1>
      
        <h2 id="subtitle-wrap">
          <a href="/" id="subtitle">yywei的遇坑记录</a>
        </h2>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS 订阅"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="搜索"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="搜索"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://gitee.com/weiyayun1995/weiyayun1995.git"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-js数据类型及转换" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2021/06/22/js%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%8F%8A%E8%BD%AC%E6%8D%A2/" class="article-date">
  <time class="dt-published" datetime="2021-06-22T05:42:36.000Z" itemprop="datePublished">2021-06-22</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      js数据类型及转换
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h5 id="下面代码输出结果"><a href="#下面代码输出结果" class="headerlink" title="下面代码输出结果"></a>下面代码输出结果</h5><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">[<span class="number">1</span>,<span class="number">2</span>] + [<span class="number">3</span>,<span class="number">4</span>]</span><br><span class="line">[] + &#123;&#125;</span><br><span class="line">&#123;&#125; + []</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 1. []和&#123;&#125;都是对象，因此调用toString()方法，&#x27;&#x27; + &#x27;[Object Object]&#x27;</span></span><br><span class="line"><span class="comment">// 2. &#123;&#125;es6中被认为是一个代码块，+[]表示正负数因此被强制转换成数字类型+0，所以结果是0</span></span><br></pre></td></tr></table></figure>
<h5 id="基本数据类型"><a href="#基本数据类型" class="headerlink" title="基本数据类型"></a>基本数据类型</h5><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Undefined、Null、<span class="built_in">Boolean</span>、<span class="built_in">Number</span>、<span class="built_in">String</span>、<span class="built_in">Symbol</span></span><br></pre></td></tr></table></figure>
<p><img src="https://assets.che300.com/wiki/2021-05-18/16213164486292034.png"></p>
<h5 id="精度丢失的解决办法"><a href="#精度丢失的解决办法" class="headerlink" title="精度丢失的解决办法"></a>精度丢失的解决办法</h5><p>将小数放大到正数，再缩小为原来的倍数<br>例：(0.1<em>10 + 0.2</em>10) / 10</p>
<h5 id="再来看以下代码"><a href="#再来看以下代码" class="headerlink" title="再来看以下代码"></a>再来看以下代码</h5><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log([] == [])</span><br><span class="line"><span class="built_in">console</span>.log([] == ![])</span><br><span class="line"><span class="built_in">console</span>.log([] !== [])</span><br><span class="line"><span class="built_in">console</span>.log(<span class="literal">NaN</span> != <span class="literal">NaN</span>)</span><br><span class="line"><span class="built_in">console</span>.log(<span class="literal">null</span> == <span class="literal">undefined</span>)</span><br><span class="line"><span class="built_in">console</span>.log(<span class="literal">null</span> === <span class="literal">undefined</span>)</span><br><span class="line"><span class="built_in">console</span>.log(<span class="number">1</span> == <span class="literal">true</span>)</span><br><span class="line"><span class="built_in">console</span>.log(<span class="literal">null</span> &gt; <span class="number">0</span>)</span><br><span class="line"><span class="built_in">console</span>.log(<span class="literal">true</span> + <span class="number">1</span>)</span><br><span class="line"><span class="built_in">console</span>.log(<span class="literal">undefined</span> + <span class="number">1</span>)</span><br><span class="line"><span class="built_in">console</span>.log(&#123;&#125; + <span class="number">1</span>)</span><br><span class="line"><span class="built_in">console</span>.log([] + &#123;&#125;)</span><br><span class="line"><span class="built_in">console</span>.log([<span class="number">2</span>,<span class="number">3</span>] + [<span class="number">1</span>,<span class="number">2</span>])</span><br></pre></td></tr></table></figure>

<h5 id="分析下列值分别对应转化为数字、字符串、布尔时的值"><a href="#分析下列值分别对应转化为数字、字符串、布尔时的值" class="headerlink" title="分析下列值分别对应转化为数字、字符串、布尔时的值"></a>分析下列值分别对应转化为数字、字符串、布尔时的值</h5><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="literal">false</span>,<span class="literal">true</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="string">&quot;0&quot;</span>,<span class="literal">NaN</span>,<span class="literal">Infinity</span>（表示无穷大）,<span class="string">&quot;&quot;</span>,[],&#123;&#125;,[<span class="number">0</span>],<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;&#125;,<span class="literal">null</span>,undefiend</span><br></pre></td></tr></table></figure>
<h5 id="对应值"><a href="#对应值" class="headerlink" title="对应值"></a>对应值</h5><p><img src="https://assets.che300.com/wiki/2021-05-09/16205717674285892.png"></p>
<h5 id="正负运算符"><a href="#正负运算符" class="headerlink" title="正负运算符"></a>正负运算符</h5><p>仅仅表示数字的正负，注意和+运算符的区分</p>
<h4 id="比较运算"><a href="#比较运算" class="headerlink" title="比较运算"></a>比较运算</h4><p>js为我们提供了严格比较与类型转换比较两种模式，严格比较(===)只会在操作符两侧的操作对象类型一致，并且内容一致时才会返回true，否则返回false。而更为广泛使用的==操作符则会首先将操作对象转化为相同类型，在进行比较。</p>
<ol>
<li><p><strong>相等操作符会对操作值进行隐式转换后进行比较：</strong><br>如果一个操作值为布尔值，则在比较之前先将其转换为数值<br>如果一个操作值为字符串，另一个操作值为数值，则通过Number()函数将字符串转换为数值<br>如果一个操作值是对象，另一个不是，则调用对象的valueOf()方法，得到的结果按照前面的规则进行比较<br>null与undefined是相等的<br>如果一个操作值为NaN，则相等比较返回false<br>如果两个操作值都是对象，则比较它们是不是指向同一个对象</p>
</li>
<li><p><strong>加法运算操作符:</strong><br>加法运算符在js中也用作字符串拼接，所以加法操作符的规则分为两种：<br>如果一个数是NaN，则结果就是NaN<br>如果是Infinity+Infinity，结果是Infinity<br>如果是-Infinity+(-Infinity)，结果是-Infinity<br>如果是Infinity+(-Infinity)，结果是NaN<br>如果有一个操作值是字符串，则：<br>如果两个都是字符串则，拼接起来<br>一个有一个操作值是字符串，则将另外的值转换成字符串，然后拼接起来<br>如果又一个值是对象、数值或者布尔值，则调用toString()方法取得字符串值，然后再应用前面的字符串规则。对于undefined和null，分别调用String()显式转换为字符串。</p>
</li>
<li><p><strong>逻辑操作符（!、&amp;&amp;、||）</strong><br>逻辑非（！）操作符首先通过Boolean()函数将它的操作值转换为布尔值，然后求反。<br>逻辑与（&amp;&amp;）操作符，如果一个操作值不是布尔值时，遵循以下规则进行转换：<br>如果有一个操作值为null，返回null<br>如果有一个操作值为NaN，返回NaN<br>如果有一个操作值为undefined，返回undefined<br>逻辑或（||）操作符，如果一个操作值不是布尔值，遵循以下规则：<br>如果第一个操作值经Boolean()转换后为false，则返回第二个操作值，否则返回第一个操作值（不是Boolean()转换后的值）<br>对于undefined、null和NaN的处理规则与逻辑与（&amp;&amp;）相同</p>
</li>
<li><p><strong>关系操作符（&lt;, &gt;, &lt;=, &gt;=）</strong><br>与上述操作符一样，关系操作符的操作值也可以是任意类型的，所以使用非数值类型参与比较时也需要系统进行隐式类型转换：<br>如果两个操作值都是数值，则进行数值比较<br>如果两个操作值都是字符串，则比较字符串对应的字符编码值<br>如果只有一个操作值是数值，则将另一个操作值转换为数值，进行数值比较<br>如果一个操作数是对象，则调用valueOf()方法（如果对象没有valueOf()方法则调用toString()方法），得到的结果按照前面的规则执行比较<br>如果一个操作值是布尔值，则将其转换为数值，再进行比较</p>
</li>
</ol>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://gitee.com/weiyayun1995/weiyayun1995.git/2021/06/22/js%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%8F%8A%E8%BD%AC%E6%8D%A2/" data-id="ckq7mlsv90009dgt96jmh89rl" data-title="js数据类型及转换" class="article-share-link">分享</a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
  
    <a href="/2021/06/18/react-hook-%E9%A1%B9%E7%9B%AE%E4%B8%AD%E5%AE%9E%E8%B7%B5/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">后一篇</strong>
      <div class="article-nav-title">react-hook 项目中实践</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/06/">六月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/05/">五月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/04/">四月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/03/">三月 2021</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/06/22/js%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%8F%8A%E8%BD%AC%E6%8D%A2/">js数据类型及转换</a>
          </li>
        
          <li>
            <a href="/2021/06/18/react-hook-%E9%A1%B9%E7%9B%AE%E4%B8%AD%E5%AE%9E%E8%B7%B5/">react-hook 项目中实践</a>
          </li>
        
          <li>
            <a href="/2021/06/18/%E6%95%B0%E5%AD%97%E7%AD%BE%E5%90%8D/">数字签名</a>
          </li>
        
          <li>
            <a href="/2021/06/18/sendbeacon/">sendbeacon</a>
          </li>
        
          <li>
            <a href="/2021/06/18/js%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E7%9A%84%E5%88%A4%E6%96%AD/">js数据类型的判断</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2021 yywei<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.4.1.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>